<template>
  <div class="app-container">
    <el-form ref="form" :model="user" :rules="rules" label-width="100px">
      <el-form-item label="用户昵称" prop="nickName">
        <el-input v-model="user.nickName" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="user.phone" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="user.email" maxlength="30"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="user.sex">
          <el-radio v-for="status in sexOptions" :key="status.dictValue" :label="status.dictValue">
            {{ status.dictLabel }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item >
        <el-button type="primary" size="mini" @click="doSubmit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {getInfo} from "@/api/user";
import sysUserApi from "@/api/system/sysUser";

export default {
  name: "userInfo",
  data(){
    return{
      user:{},
      //性别的字典数据
      sexOptions: [],
      //验证规则
      rules:{
        nickName: [
          {required: true, message: '用户昵称不能空', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '邮箱地址不能为空', trigger: 'blur'},
          { type:"email",message: "请输入正确的邮箱地址", trigger: ['blur','change'] }
        ],
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          { min: 11, max: 11, message: '手机号必须是11个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getDataByType("sys_user_sex").then(res => {
      this.sexOptions = res.data;
    })
    getInfo().then(res=>{
      this.user=res.data.user;
    })
  },
  methods:{
    doSubmit(){
      this.$refs["form"].validate(valid=>{
        if(valid){
          sysUserApi.update(this.user).then(res=>{
            this.$message.success("修改成功")
            this.$emit("after")
          })
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
